//
//  YYRegisterViewController.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/7/29.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYRegisterViewController.h"
#import "YYWebViewVC.h"

@interface YYRegisterViewController ()

@end

@implementation YYRegisterViewController{
    BOOL _isAgree;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _isAgree = YES;
    self.phoneView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.codeView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.inviteView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.passwordView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.bgImageView.image = DEF_IMAGENAME(@"login_bg");
    [self.tfPhone addTarget:self action:@selector(tfDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void)tfDidChange:(UITextField *)tf{
    if ([tf isEqual:self.tfPhone]) {
        if (tf.text.length>11) {
            tf.text = [tf.text substringToIndex:11];
        }
    }
    
}

- (IBAction)btnCode:(id)sender {
    if (self.tfPhone.text.length!=11) {
        [AlertUtil alertMessage:@"请输入正确格式的手机号码"];
        return;
    }
    
    [self.btnCode startTime];
    [RequestOperationManager sendCode_action:@"register" phone:self.tfPhone.text Success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            [AlertUtil alertMessage:@"验证码已发送到您的手机"];
        }
        
    } failture:^(NSError *error) {
        
    }];
}

- (IBAction)back:(id)sender {
     [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)register:(id)sender {
    if (self.tfPhone.text.length!=11) {
        [AlertUtil alertMessage:@"请输入正确格式的手机号码"];
        return;
    }
    if (self.tfPassword.text.length==0) {
        [AlertUtil alertMessage:@"请输入密码"];
        return;
    }
    
    if (self.tfCode.text.length==0) {
        [AlertUtil alertMessage:@"验证码"];
        return;
    }
    
    if (!_isAgree) {
        [AlertUtil alertMessage:@"请先同意《嘀车惠服务协议》"];
        return;
    }
    
    [RequestOperationManager register_iphone:self.tfPhone.text password:self.tfPassword.text source_code:self.tfInvite.text code:self.tfCode.text Success:^(id result) {
        if(DEF_REQ_SUCCESS){
            [AlertUtil alertSuccess:@"注册成功"];
               [[NSUserDefaults standardUserDefaults] setObject:result[@"data"][@"uid"] forKey:@"uid"];
            [[NSUserDefaults standardUserDefaults] setObject:self.tfPhone.text forKey:@"phone"];
            [[AppDelegate shareAppDelegate] enterHomePageVC];
        }
        
        
    } failture:^(NSError *error) {
        
    }];
}
- (IBAction)agreeClicked:(id)sender {
    _isAgree = !_isAgree;
    if (_isAgree) {
        [self.btnAgree setImage:DEF_IMAGENAME(@"duihao") forState:UIControlStateNormal];

    }else{
        [self.btnAgree setImage:DEF_IMAGENAME(@"sign_inspection vehicle_checkbox_anselected") forState:UIControlStateNormal];

    }
}

- (IBAction)toAgreement:(id)sender {
    YYWebViewVC * web = [[YYWebViewVC alloc] init];
    web.title = @"服务协议";
    [web loadUrl:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,@"/api/login/agreement"]];
    [self.navigationController pushViewController:web animated:YES];
}



@end
